package io.github.talelin.latticy.controller.v1;

import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.service.SalesPurchaseStatsService;
import io.github.talelin.latticy.vo.SalesPurchaseStatsVO;
import io.github.talelin.latticy.vo.SalesPurchaseRecordVO;
import io.github.talelin.latticy.dto.SalesPurchaseStatsQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.HashMap;
import java.util.Map;

/**
 * 销售购买金额统计控制器
 */
@RestController
@RequestMapping("/v1/sales-purchase-stats")
@Validated
@PermissionModule("销售购买统计")
public class SalesPurchaseStatsController {

    @Autowired
    private SalesPurchaseStatsService salesPurchaseStatsService;

    /**
     * 获取销售购买统计数据
     */
    @GetMapping("/stats")
    @GroupRequired
    @PermissionMeta("查询统计数据")
    public SalesPurchaseStatsVO getStats(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        
        return salesPurchaseStatsService.getStats(startDate, endDate);
    }

    /**
     * 获取销售购买记录列表
     */
    @GetMapping("/records")
    @GroupRequired
    @PermissionMeta("查询记录列表")
    public PageResponseVO<SalesPurchaseRecordVO> getRecords(
            @RequestParam(name = "page", defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", defaultValue = "20")
            @Min(value = 1, message = "{count.min}")
            @Max(value = 100, message = "{count.max}") Integer count,
            @RequestParam(name = "type", required = false) String type,
            @RequestParam(name = "status", required = false) String status,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(name = "sortBy", required = false) String sortBy,
            @RequestParam(name = "sortOrder", required = false) String sortOrder) {
        
        SalesPurchaseStatsQueryDTO queryDTO = new SalesPurchaseStatsQueryDTO();
        queryDTO.setPage(page);
        queryDTO.setCount(count);
        queryDTO.setType(type);
        queryDTO.setStatus(status);
        queryDTO.setKeyword(keyword);
        queryDTO.setStartDate(startDate);
        queryDTO.setEndDate(endDate);
        queryDTO.setSortBy(sortBy);
        queryDTO.setSortOrder(sortOrder);
        
        return salesPurchaseStatsService.getRecords(queryDTO);
    }

    /**
     * 获取销售趋势数据
     */
    @GetMapping("/sales-trend")
    @GroupRequired
    @PermissionMeta("查询销售趋势")
    public Map<String, Object> getSalesTrend(
            @RequestParam(name = "type", defaultValue = "daily") String type,
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        
        return salesPurchaseStatsService.getSalesTrend(type, startDate, endDate);
    }

    /**
     * 获取购买趋势数据
     */
    @GetMapping("/purchase-trend")
    @GroupRequired
    @PermissionMeta("查询购买趋势")
    public Map<String, Object> getPurchaseTrend(
            @RequestParam(name = "type", defaultValue = "daily") String type,
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        
        return salesPurchaseStatsService.getPurchaseTrend(type, startDate, endDate);
    }

    /**
     * 导出统计数据
     */
    @PostMapping("/export")
    @GroupRequired
    @PermissionMeta("导出统计数据")
    public Map<String, Object> exportData(@RequestBody @Validated SalesPurchaseStatsQueryDTO queryDTO) {
        
        String downloadUrl = salesPurchaseStatsService.exportData(queryDTO);
        Map<String, Object> result = new HashMap<>();
        result.put("downloadUrl", downloadUrl);
        result.put("message", "导出成功");
        return result;
    }

    /**
     * 获取统计概览
     */
    @GetMapping("/overview")
    @GroupRequired
    @PermissionMeta("查询统计概览")
    public Map<String, Object> getOverview(
            @RequestParam(name = "period", defaultValue = "month") String period) {
        
        return salesPurchaseStatsService.getOverview(period);
    }

    /**
     * 测试数据库连接
     */
    @GetMapping("/test-db")
    @GroupRequired
    @PermissionMeta("测试数据库连接")
    public Map<String, Object> testDatabaseConnection() {
        
        return salesPurchaseStatsService.testDatabaseConnection();
    }

    /**
     * 测试已付款订单查询
     */
    @GetMapping("/test-paid-orders")
    @GroupRequired
    @PermissionMeta("测试已付款订单查询")
    public Map<String, Object> testPaidOrdersQuery() {
        
        return salesPurchaseStatsService.testPaidOrdersQuery();
    }
} 